package cn.ygh.bbo.system.web.controller;

import cn.ygh.bbo.framerwork.core.ClientInfo;
import cn.ygh.bbo.framerwork.core.Grid;
import cn.ygh.bbo.framerwork.core.ResultAjax;
import cn.ygh.bbo.common.exception.TipException;
import cn.ygh.bbo.common.utils.ExcelUtils;
import cn.ygh.bbo.common.utils.FileUtils;
import cn.ygh.bbo.framerwork.config.properties.DynamicProperties;
import cn.ygh.bbo.system.bean.OpretationLog;
import cn.ygh.bbo.system.service.OpretationLogService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 描述：操作日志  RestPageController
 * @author ygh
 */
@Controller
@RequestMapping("/restpage/opretationLog")
public class OpretationLogController {

    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    OpretationLogService opretationLogService;

    @Autowired
    DynamicProperties dynamicProperties;

    /**
     * 查询列表api 带总数
     * @param request 请求
     * @param response 响应
     * @param clientInfo 查询条件
     * @return
     */
    @RequiresPermissions("system:opretationLog:query")
    @ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Grid grid(HttpServletRequest request, HttpServletResponse response, ClientInfo clientInfo, OpretationLog search){
        search.setOrderValue(clientInfo.realSort().trim());
        if (clientInfo.getPageNo()==0){
            throw new TipException("pageNum can not zero");
        }
        Grid grid=opretationLogService.listGrid(search,PageRequest.of(clientInfo.getPageNo(),clientInfo.getPageSize()));
        return grid;
    }

    /**
     * 详细
     * @param id
     * @return
     */
    @RequiresPermissions("system:opretationLog:show")
    @ResponseBody
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public OpretationLog get(HttpServletRequest request, HttpServletResponse response, Integer id){
        OpretationLog result=opretationLogService.getById(id);
        return result;
    }

    /**
     * 获取数据表格Excel版，这个会把所有的符合条件的进行返回
     * @param request 请求
     * @param response 响应
     * @param obj
     */
    @RequiresPermissions("OpretationLog:query")
    @RequestMapping(value = "/excelexport",method = RequestMethod.GET)
    @ResponseBody
    public ResultAjax excelexport(HttpServletRequest request,HttpServletResponse response,OpretationLog obj) throws IllegalAccessException {
        //获取数据
        List<OpretationLog>opretationLogs = opretationLogService.listfind(obj,null);
        //生成excel文档
        Workbook excel = ExcelUtils.listToWorkbook(opretationLogs, OpretationLog.class);
        //保存在本地
        String s = FileUtils.fileExcelSave(dynamicProperties.getFilePath(), excel);;
        //返回下载地址
        return ResultAjax.success(dynamicProperties.getContextPath()+s);
    }
}
